package com.fizzkai.p2p.website.controller;

import com.fizzkai.p2p.bussness.domain.BidRequest;
import com.fizzkai.p2p.bussness.domain.ExpAccount;
import com.fizzkai.p2p.bussness.service.IBidRequestService;
import com.fizzkai.p2p.bussness.service.IExpAccountService;
import com.fizzkai.p2p.core.domain.Account;
import com.fizzkai.p2p.core.domain.LoginInfo;
import com.fizzkai.p2p.core.domain.UserInfo;
import com.fizzkai.p2p.core.service.IAccountService;
import com.fizzkai.p2p.core.service.IRealAuthService;
import com.fizzkai.p2p.core.service.IUserInfoService;
import com.fizzkai.p2p.core.util.AjaxResult;
import com.fizzkai.p2p.core.util.Constants;
import com.fizzkai.p2p.core.util.UserContext;
import com.fizzkai.p2p.website.util.RequireLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
public class BidRequestController {

    @Autowired
    private IAccountService accountService;
    @Autowired
    private IExpAccountService expAccountService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IRealAuthService realAuthService;

    /**
     * 申请借款 页面
     * @param model
     * @return
     */
    @RequestMapping("/borrowInfo")
    @RequireLogin
    public String borrowInfo(Model model){
        // 获取 当前用户
        UserInfo userInfo = userInfoService.getCurrent();
        // 当前用户 没有通过验证 重定向到 我要借款页面
        if (!userInfo.isCanBorrow()) {
            return "redirect:borrow";
        }
        if (userInfo.isHasBidRequestProcess()) {
            return "borrow_apply_result";
        }
        Account account = accountService.getCurrent();
        // 最小 贷款金额
        model.addAttribute("minBidRequestAmount", Constants.BORROW_AMOUNT_MIN);
        // 最小 投标金额
        model.addAttribute("minBidAmount", Constants.BID_AMOUNT_MIN);
        model.addAttribute("account", account);
        return "borrow_apply";
    }

    /**
     * 申请 借款
     * @return
     */
    @RequestMapping("/borrow_apply")
    @ResponseBody
    public Object borrow_apply(BidRequest br){
        bidRequestService.apply(br);
        return new AjaxResult("申请借款成功");
    }

    /**
     * 借款详情页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/borrow_info")
    public String borrow_info(Long id, Model model){
        // bidRequest
        BidRequest bidRequest = bidRequestService.get(id);
        model.addAttribute("bidRequest", bidRequest);
        if (bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_EXP) {
            // 体验标
            // 查出 account
            if (UserContext.getCurrentLoginInfo() != null) {
                ExpAccount expAccount = expAccountService.get(UserContext.getCurrentLoginInfo().getId());
                model.addAttribute("expAccount", expAccount);
            }
            return "exp_borrow_info";
        }
        // 普通标
        // userInfo 借款人的 createUser
        UserInfo userInfo = userInfoService.get(bidRequest.getCreateUser().getId());
        model.addAttribute("userInfo", userInfo);
        // realAuth 借款人 信息
        model.addAttribute("realAuth", realAuthService.get(userInfo.getRealAuthId()));

        // account 自己的
        LoginInfo loginUser = UserContext.getCurrentLoginInfo();
        if (loginUser == null) {
            // 未登录
            model.addAttribute("self", false);
            return "borrow_info1";
        }
        // 登录了 是自己
        if (loginUser.getId().equals(bidRequest.getCreateUser().getId())) {

            model.addAttribute("self", true);
            return "borrow_info1";
        }
        // 不是 自己
        model.addAttribute("self", false);
        model.addAttribute("account", accountService.getCurrent());
        return "borrow_info1";
    }

    /**
     * 投标 请求
     * @return
     */
    @RequestMapping("/borrow_bid")
    @ResponseBody
    public Object borrow_bid(Long bidRequestId, BigDecimal amount){
        bidRequestService.bid(bidRequestId, amount);
        return new AjaxResult("投标成功");
    }
}
